package com.xb.hehe.app.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xb.hehe.app.dto.TLoanPlatformInfoDTO;
import com.xb.hehe.app.mapper.TLoanPlatformInfoMapper;
import com.xb.hehe.app.model.TLoanPlatformInfo;
import com.xb.hehe.app.service.TLoanPlatformInfoService;
import com.xb.loan.util.exception.ServiceDataException;
import com.xb.loan.util.page.Page;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * 贷款平台申请流程、条件、资料信息配置业务层访问接口实现
 * @author  wuql
 * @version tLoanPlatformInfoServiceImpl.java, v 0.1  2017-11-6 15:10:01
 */
@Service("tLoanPlatformInfoService")
public class TLoanPlatformInfoServiceImpl implements TLoanPlatformInfoService {

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TLoanPlatformInfoServiceImpl.class);
    /**贷款平台申请流程、条件、资料信息配置数据层访问接口实现**/
	@Autowired
	private TLoanPlatformInfoMapper tLoanPlatformInfoMapper;

    @Override
    public Long saveTLoanPlatformInfo(TLoanPlatformInfoDTO tLoanPlatformInfoDTO) {
        logger.info("saveTLoanPlatformInfo tLoanPlatformInfoDTO:{}", tLoanPlatformInfoDTO);
        long rows = 0;
        try {
            if (tLoanPlatformInfoDTO != null) {
                TLoanPlatformInfo tLoanPlatformInfo = new TLoanPlatformInfo();
                BeanUtils.copyProperties(tLoanPlatformInfoDTO, tLoanPlatformInfo);
                if (tLoanPlatformInfo.getId() != null && tLoanPlatformInfo.getId() != 0) {
                    //更新
                    rows = tLoanPlatformInfoMapper.updateByPrimaryKeySelective(tLoanPlatformInfo);
                } else {
                    //插入
                    tLoanPlatformInfo.setCreateTime(System.currentTimeMillis());
                    rows = tLoanPlatformInfoMapper.insert(tLoanPlatformInfo);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceDataException("saveTLoanPlatformInfo Error::", e);
        }
        return rows;
    }

       @Override
       public Long deleteTLoanPlatformInfo(Long id) {
       logger.info("deleteTLoanPlatformInfo tLoanPlatformInfoId:{}", id);
           long rows = 0;
           try {
               rows = tLoanPlatformInfoMapper.deleteByPrimaryKey(id);
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("deletetLoanPlatformInfo Error::", e);
           }
           return rows;
       }

       @Override
       public TLoanPlatformInfoDTO queryTLoanPlatformInfoById(Long id) {
       logger.info("queryTLoanPlatformInfoById tLoanPlatformInfoId:{}", id);
           TLoanPlatformInfoDTO tLoanPlatformInfoDTO = null;
           try {
              TLoanPlatformInfo tLoanPlatformInfo = tLoanPlatformInfoMapper.selectByPrimaryKey(id);
                           if(tLoanPlatformInfo!=null){
                           tLoanPlatformInfoDTO = new TLoanPlatformInfoDTO();
                               BeanUtils.copyProperties(tLoanPlatformInfo,tLoanPlatformInfoDTO);
                           }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("querytLoanPlatformInfoById", e);
           }
           return tLoanPlatformInfoDTO;
       }

       @Override
       public List<TLoanPlatformInfoDTO> queryTLoanPlatformInfo(TLoanPlatformInfoDTO tLoanPlatformInfoDTO) {
            logger.info("queryTLoanPlatformInfo tLoanPlatformInfoDTO:{}", tLoanPlatformInfoDTO);
                  List<TLoanPlatformInfoDTO> list = null;
                  List<TLoanPlatformInfo> tLoanPlatformInfoList = null;
                  try {
                      if (tLoanPlatformInfoDTO != null) {
                          TLoanPlatformInfo tLoanPlatformInfo = new TLoanPlatformInfo();
                          BeanUtils.copyProperties(tLoanPlatformInfoDTO, tLoanPlatformInfo);
                          tLoanPlatformInfoList = tLoanPlatformInfoMapper.select(tLoanPlatformInfo);
                      } else {
                          Example example = new Example(TLoanPlatformInfo.class);
                          // 排序规则
                          example.orderBy("id").desc();
                          tLoanPlatformInfoList = tLoanPlatformInfoMapper.selectByExample(example);
                      }
                      if (tLoanPlatformInfoList != null && tLoanPlatformInfoList.size() > 0) {
                          list = convertTLoanPlatformInfoDTO(tLoanPlatformInfoList);
                      }

                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                  }
                  return list;
       }

       @Override
       public Page<TLoanPlatformInfoDTO> queryTLoanPlatformInfoPage(TLoanPlatformInfoDTO tLoanPlatformInfoDTO) {
        logger.info("queryTLoanPlatformInfoPage tLoanPlatformInfoDTO:{}", tLoanPlatformInfoDTO);
           Page<TLoanPlatformInfoDTO> page = null;
           Integer currentPage = tLoanPlatformInfoDTO.getPage();
           if (currentPage < 1) currentPage = 1;
           try {
               page = new Page<TLoanPlatformInfoDTO>(currentPage, tLoanPlatformInfoDTO.getPageCount());
               PageHelper.startPage(currentPage, tLoanPlatformInfoDTO.getPageCount());
               Example example = new Example(TLoanPlatformInfo.class);
               // 排序规则
               example.orderBy("id").desc();
               List<TLoanPlatformInfo> list = tLoanPlatformInfoMapper.selectByExample(example);

               if (list != null && list.size() > 0) {
                   PageInfo pageInfo = new PageInfo(list);
                   page.setTotalCount(Long.valueOf(pageInfo.getTotal()).intValue());
                    List<TLoanPlatformInfoDTO> tLoanPlatformInfoDTOList = convertTLoanPlatformInfoDTO(list);
                   page.setResult(tLoanPlatformInfoDTOList);
               }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
           }

           return page;
       }
	  private List<TLoanPlatformInfoDTO> convertTLoanPlatformInfoDTO(List<TLoanPlatformInfo> tLoanPlatformInfoList) {
            
            if (tLoanPlatformInfoList == null) return null;
             List<TLoanPlatformInfoDTO> tLoanPlatformInfoDTOs = new ArrayList<>();
             for (TLoanPlatformInfo tLoanPlatformInfo : tLoanPlatformInfoList
                     ) {
                 TLoanPlatformInfoDTO tLoanPlatformInfoDTO = new TLoanPlatformInfoDTO();
                 BeanUtils.copyProperties(tLoanPlatformInfo, tLoanPlatformInfoDTO);
                 tLoanPlatformInfoDTOs.add(tLoanPlatformInfoDTO);
             }
             return tLoanPlatformInfoDTOs;
         }
}